package com.yj.easydialog.dialog;

import android.app.Dialog;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.StyleRes;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;

import com.yj.easydialog.R;

/**
 * Created by Sigthp-P-Department on 2018/4/4.
 */

public class AlertDialog extends Dialog {

    private AlertController mAlert;

    public AlertDialog(@NonNull Context context, @StyleRes int themeResId) {
        super(context, themeResId);
        mAlert=new AlertController(this,getWindow());
    }

    public void setTextView(int viewId, CharSequence charSequence) {
        mAlert.setTextView(viewId,charSequence);
    }

    public void setOnClickLisner(int viewId, View.OnClickListener onClickListener) {
        mAlert.setOnClickLisner(viewId,onClickListener);
    }

    public <T extends View>T getView(int viewId) {
        return mAlert.getView(viewId);
    }

    //设计核心Builder用于存放数据等
    public static class Builder{

        private AlertController.AlertParams P;

        public Builder(Context context){
            this(context, R.style.BaseDialogTheme);
        }

        public  Builder(Context context,int themeResId){
            P = new AlertController.AlertParams(context,themeResId);
        }
        private AlertDialog.Builder setText(int viewID,CharSequence charSequence){
            P.mTextViewArray.put(viewID,charSequence);
            return this;
        }

        private AlertDialog.Builder setOnClickListener(int viewID, View.OnClickListener onClickListener){
            P.mClickArray.put(viewID,onClickListener);
            return this;
        }

        public AlertDialog.Builder setContentView(int layoutResId) {
            P.mView = null;
            P.mViewLayoutResId = layoutResId;
            return this;
        }

        public AlertDialog.Builder setContentView(View view) {
            P.mView = view;
            P.mViewLayoutResId = 0;
            return this;
        }

        public AlertDialog.Builder setFullScreen() {
            P.mWidth = ViewGroup.LayoutParams.MATCH_PARENT;
            return this;
        }

        public AlertDialog.Builder setShowButtom(int animationId){
            if(animationId!=0){
                P.mAnimationId=animationId;
            }
            P.mGravity= Gravity.BOTTOM;
            return this;
        }


        public AlertDialog.Builder setShowButtom(){
            P.mGravity= Gravity.BOTTOM;
            return this;
        }
        //
        public AlertDialog.Builder setWindowAnimation(int animationId){
            if(animationId!=0){
                P.mAnimationId=animationId;
            }
            return this;
        }

        //
        public AlertDialog.Builder setDailogWidthAndHeight(int width,int height){
            P.mWidth = width;
            P.mHeight = height;
            return this;
        }

        //1
        public AlertDialog.Builder setCancelable(boolean cancelable) {
            P.mCancelable = cancelable;
            return this;
        }
        //2
        public AlertDialog.Builder setCanceledOnTouchOutside(boolean canceledOnTouchOutside) {
            P.mCanceledOnTouchOutside = canceledOnTouchOutside;
            return this;
        }
        //3
        public AlertDialog.Builder setOnCancelListener(OnCancelListener onCancelListener) {
            P.mOnCancelListener = onCancelListener;
            return this;
        }
        //4
        public AlertDialog.Builder setOnDismissListener(OnDismissListener onDismissListener) {
            P.mOnDismissListener = onDismissListener;
            return this;
        }
        //5
        public AlertDialog.Builder setOnKeyListener(OnKeyListener onKeyListener) {
            P.mOnKeyListener = onKeyListener;
            return this;
        }
        public AlertDialog create(){
            AlertDialog dialog=new AlertDialog(P.mContext,P.mThemeResId);
            P.apply(dialog.mAlert);
            dialog.setCancelable(P.mCancelable);
            dialog.setCanceledOnTouchOutside(P.mCanceledOnTouchOutside);
            dialog.setOnCancelListener(P.mOnCancelListener);
            dialog.setOnDismissListener(P.mOnDismissListener);
            if (P.mOnKeyListener != null) {
                dialog.setOnKeyListener(P.mOnKeyListener);
            }
            return dialog;
        }

        public AlertDialog show() {
            final AlertDialog dialog = create();
            dialog.show();
            return dialog;
        }

    }
}
